package com.example.attendancesystem_1_8;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.os.SystemClock;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.example.attendancesystem_1_8.backendInterface.PhotoBase;
import com.example.attendancesystem_1_8.backendInterface.RegisterThread;

import java.io.File;
import java.io.IOException;

public class RegisterActivity extends AppCompatActivity {

    private static int result = -1;//用于子线程回调
    private TextView tv_goto_login;
    private Button bt_register;
    private EditText et_account;
    private EditText et_username;
    private EditText et_password1;
    private EditText et_password2;
    private RadioGroup rg_isBoss;//员工类型单选框组
    private String account_input;
    private String username_input;
    private String password_input1;
    private String password_input2;
    private int isBoss_choose = -1;

    private static String[] PERMISSIONS_STORAGE = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA };
    private ImageView iv_first_head;        //头像预览
    private Button bt_open_camera;          //打开相机
    private Button bt_open_photo;           //打开相册
    private File tempPic = null;   //新建一个 File 文件（用于相机拿数据）

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        tv_goto_login = (TextView)findViewById(R.id.tv_go_to_login);
        bt_register = (Button)findViewById(R.id.bt_register);
        et_username = (EditText)findViewById(R.id.et_username_register);
        et_password1 = (EditText)findViewById(R.id.et_pw_1);
        et_password2 = (EditText)findViewById(R.id.et_pw_2);
        et_account = (EditText)findViewById(R.id.et_account_register);
        rg_isBoss = (RadioGroup) findViewById(R.id.rg_isBoss);


        tv_goto_login.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }



        rg_isBoss.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton radioButton = (RadioButton) radioGroup.findViewById(i);
                if(radioButton.getText().equals("员工")){
                    isBoss_choose = 0;
                    Log.i("tag","员工");
                }
                else if(radioButton.getText().equals("主管")){
                    isBoss_choose = 1;
                    Log.i("tag","主管");
                }
                else if(radioButton.getText().equals("老板")){
                    isBoss_choose = 2;
                    Log.i("tag","老板");
                }

            }
        });
        //账号输入框变化监听
        et_account.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                account_input = charSequence.toString();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        //用户名输入框变化监听
        et_username.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                username_input = charSequence.toString();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        //第一个密码输入框变化监听
        et_password1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                password_input1 = charSequence.toString();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        //第二个密码输入框变化监听
        et_password2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                password_input2 = charSequence.toString();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        //点击注册按钮事件
        bt_register.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                if(account_input == null){
                    Toast.makeText(RegisterActivity.this,"账号不能为空",Toast.LENGTH_LONG).show();
                }
                else if(username_input == null){
                    Toast.makeText(RegisterActivity.this,"用户名不能为空",Toast.LENGTH_LONG).show();
                }
                else if(password_input1 == null){
                    Toast.makeText(RegisterActivity.this,"密码不能为空",Toast.LENGTH_LONG).show();
                }
                else if(password_input2 == null){
                    Toast.makeText(RegisterActivity.this,"确认密码框不能为空",Toast.LENGTH_LONG).show();
                }
                else if(account_input.length() < 6 || account_input.length() > 18){
                    Toast.makeText(RegisterActivity.this,"账号应为6-18个字符",Toast.LENGTH_LONG).show();
                }
                else if(username_input.length() < 6 || username_input.length() > 18){
                    Toast.makeText(RegisterActivity.this,"用户名应为6-18个字符",Toast.LENGTH_LONG).show();
                }
                else if(password_input1.length() < 6 || password_input1.length() > 18){
                    Toast.makeText(RegisterActivity.this,"密码应为6-18个字符",Toast.LENGTH_LONG).show();
                }
                else if(!password_input1.equals(password_input2)){
                    Toast.makeText(RegisterActivity.this,"两次密码不同",Toast.LENGTH_LONG).show();
                }
                else if(isBoss_choose == -1){
                    Toast.makeText(RegisterActivity.this,"请选择用户身份",Toast.LENGTH_LONG).show();
                }
                else{
                    Thread thread=new Thread(new RegisterThread(account_input, username_input, password_input1, isBoss_choose),"threadName");
                    thread.start();
                    try {
                        thread.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    switch (result){
                        case 0:
                            Toast.makeText(RegisterActivity.this,"注册成功，即将跳转",Toast.LENGTH_LONG).show();
                            SystemClock.sleep(2000);
                            Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                            startActivity(intent);
                            break;
                        case 2:
                            Toast.makeText(RegisterActivity.this,"账号已存在，注册失败",Toast.LENGTH_LONG).show();
                            break;
                        case 3:
                            Toast.makeText(RegisterActivity.this,"数据库内部原因导致注册失败",Toast.LENGTH_LONG).show();
                            break;
                    }
                    result = -1;
                }
            }
        });

    }
    //回调函数
    public static void callback(int backtype){
        result = backtype;
    }


}